package com.neuedu.studentmanage.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.Teacher;
import com.neuedu.studentmanage.intercepter.Const;
import com.neuedu.studentmanage.service.TeacherService;

@Controller
public class TeacherController {

	@Autowired
	private TeacherService teacherService;

	@RequestMapping("/teacher/list")
	public String doindex() {
		return "admin/teacherList";
	}

	@RequestMapping("admin/teacherList")
	@ResponseBody
	public Pager<Teacher> doList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		Pager<Teacher> pager = new Pager<>(page, rows);

		List<Teacher> list = teacherService.findListByPager(pager);
		Integer total = teacherService.findTotalByPager(pager);

		pager.setRows(list);
		pager.setTotal(total);

		return pager;
	}

	@RequestMapping("teacher/listAll")
	@ResponseBody
	public List<Teacher> doFindAll() {
		List<Teacher> teacherList = teacherService.findAll();
		return teacherList;
	}

	@RequestMapping("teacher/add")
	@ResponseBody
	public Result doAdd(Teacher teacher) {
		Result result = teacherService.add(teacher);
		return result;
	}

	@RequestMapping("teacher/edit")
	@ResponseBody
	public Result doEdit(Teacher teacher) {
		Result result = teacherService.edit(teacher);
		return result;
	}

	@RequestMapping("teacherInfo/edit")
	public String doEdit2(Teacher teacher) {
		teacherService.edit(teacher);
		return "redirect:/teacher";
	}

	@RequestMapping("teacher/passEdit")
	@ResponseBody
	public Result passEdit(String password, String ackPassword, String name, Model model) {
		Teacher teacher = new Teacher(null, password, name);
		System.out.println(teacher);
		Result result = new Result(false, "修改密码失败");
		Teacher teacher1 = teacherService.login(teacher);
		System.out.println(teacher1);
		if (teacher1 == null) {
			result = new Result(false, "修改失败，原密码错误！");
		} else {
			teacher1.setPassword(ackPassword);
			result = teacherService.passEdit(teacher1);
		}
		return result;
	}

	@RequestMapping("teacher/delete")
	@ResponseBody
	public Result doDelete(Integer id) {
		List<Teacher> list = new ArrayList<Teacher>();
		list.add(new Teacher(id));

		Result result = teacherService.deleteByIds(list);
		return result;
	}

	@RequestMapping("/teacher/info")
	public String doInfo(HttpServletRequest request, Model model) {
		String name = (String) request.getSession().getAttribute(Const.SESSION_USER);
		Teacher teacher = new Teacher(null, null, name);

		List<Teacher> teacherInfo = teacherService.findBy(teacher);
		System.out.println("teacherInfo:" + teacherInfo.get(0));
		model.addAttribute("teacherInfo", teacherInfo.get(0));
		return "teacher/teacherInfo";
	}

}
